package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.query.TravelCommendQuery;
import cn.wolfcode.trip.base.query.TravelQuery;
import cn.wolfcode.trip.base.service.ITravelCommendService;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/travels")
public class TravelController {
    @Autowired
    private ITravelService travelService;
    @Autowired
    private ITravelCommendService travelCommendService;

    /**
     * 保存或编辑游记
     * @param travel 游记信息
     * @return
     */
    @PostMapping
    public Object save(Travel travel){
        travelService.saveOrUpdate(travel);
        return new JSONResult();
    }

    /**
     * 找到特定游记的接口
     * @param id 游记id值
     * @return 游记数据
     */
    @GetMapping("/{id}")
    public Object selectById(@PathVariable Long id){
        return travelService.selectById(id);
    }

    /**
     * 更改游记的接口
     * @param travel 游记内容
     * @return
     */
    @PutMapping
    public Object update(Travel travel){
        travelService.saveOrUpdate(travel);
        return new JSONResult();
    }

    /**
     * 查找所有游记的接口
     * @return
     */
    @GetMapping
    public Object list(TravelQuery qo){
        qo.setOrderBy("tr.releaseTime desc");
        return travelService.query(qo);
    }

    /**
     * 查找推荐游记
     * @param qo 查询信息
     * @return
     */
    @GetMapping("/commends")
    public Object selectTravelCommends(TravelCommendQuery qo){
        qo.setOrderBy("com.schedule desc");
        qo.setBeforeToday(true);
        return travelCommendService.queryForApp(qo);
    }

}
